﻿using JaegerDemo.Shared.Options;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;

namespace JaegerDemo.Shared;

public static class MasstransitExtensions
{
    public static void AddMasstransitEventBus(this IServiceCollection services, RabbitMQOptions rabbitMQOptions, EventBusConfigures eventBusConfigures)
    {
        services.AddMassTransit(x =>
        {
            x.UsingRabbitMq((context, cfg) =>
            {
                cfg.Host(rabbitMQOptions.Host, ushort.Parse(rabbitMQOptions.Port), rabbitMQOptions.VirtualHost, h =>
                {
                    h.Username(rabbitMQOptions.UserName);
                    h.Password(rabbitMQOptions.Password);
                });

                eventBusConfigures?.RabbitMQConfigure?.Invoke(context, cfg);
            });

            eventBusConfigures?.MasstransitConfigure?.Invoke(x);
        });
    }

    public class EventBusConfigures
    {
        public Action<IBusRegistrationConfigurator> MasstransitConfigure;
        public Action<IBusRegistrationContext, IRabbitMqBusFactoryConfigurator> RabbitMQConfigure;
    }
}